const axios = require('axios')
const { APPID, SECRET } = require('../conf/constant')

class Token {
    static _instance = null
    constructor () {
        this.accessToken = ''
        this.updateToken()
    }
    static getInstance() {
        if(Token._instance) return Token._instance
        return Token._instance = new Token()
    }
    getAccessToken () {
        if(this.accessToken) return this.accessToken
    }
    updateToken () {
        let _this = this
        let _time = 0
        setInterval(function () {
            if(_time == 0) _time + 3600 * 1000
            axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${SECRET}`).then(res => {
                _this.accessToken = res.data.access_token
            })
        }, _time)
    }

}


module.exports = Token